package com.example.administrator.szb.http;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.example.administrator.szb.R;
import com.example.administrator.szb.bean.Messages;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.okhttps.BaseCallBack;
import com.example.administrator.szb.http.okhttps.BaseOkHttpClient;
import com.example.administrator.szb.http.okhttps.OkHttpManager;
import com.example.administrator.szb.util.Contants;
import com.example.administrator.szb.util.NetUtils;
import com.example.administrator.szb.util.QTLog;
import com.example.administrator.szb.util.SPUtils;
import com.example.administrator.szb.util.Toasts;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import okhttp3.Call;


/**
 * Http请求的工具类
 * 
 * @author 石云鹤
 * 
 */
public class HttpUtils
{
	private static final String TAG = "HttpUtils";
	private static Context context = SampleApplicationLike.getInstance();


	public static void get(Activity activity,String urlString,final OnResultListener listener){
		get(activity,urlString,0,listener);
	}

	public static void get(Activity activity,String urlString, final int reqCode, final OnResultListener listener) {
		if(!NetUtils.isConnected(context)){

			Toasts.show(activity, Contants.getString(context, R.string.net_error));
			listener.onFailure(reqCode,Contants.getString(context, R.string.net_error));
			return;
		}
		BaseOkHttpClient.Builder builder = BaseOkHttpClient.newBuilder();

		builder.url(urlString);
		builder.post();
		OkHttpManager.getInstance().tagActivity(activity).request(builder.build(), new BaseCallBack() {
			@Override
			public void onSuccess(Object o) {
				QTLog.e("onSuccess: "+o.toString() );
				Messages messages = JsonUtils.getMessages2(o.toString());
				if (messages.getResult()==1){
					if (listener!=null)
					listener.onSuccess(  reqCode, JsonUtils.JsonParse(messages));
				}else{
					if (listener!=null)
					listener.onFailure(  reqCode,messages.getErr_msg());
				}
			}

			@Override
			public void onError(int code) {
				if (listener!=null)
				listener.onFailure(reqCode,""+code);
			}

			@Override
			public void onFailure(Call call, IOException e) {
				if (listener!=null)
				listener.onFailure(reqCode,"加载失败");
			}
		});
	}
	public static void get2(Activity activity,String urlString,final int reqCode,final OnResultListener listener) {
		if(activity!=null&&!NetUtils.isConnected(activity)){

			Toasts.show(activity, Contants.getString(context, R.string.net_error));
			listener.onFailure(reqCode,Contants.getString(context, R.string.net_error));
			return;
		}
		BaseOkHttpClient.Builder builder = BaseOkHttpClient.newBuilder();
		builder.url(urlString);
		builder.post();
		OkHttpManager.getInstance().tagActivity(activity).request(builder.build(), new BaseCallBack() {
			@Override
			public void onSuccess(Object o) {
				if (listener!=null)
				listener.onSuccess(  reqCode, o.toString());
			}

			@Override
			public void onError(int code) {
				if (listener!=null)
				listener.onFailure(reqCode,""+code);
			}

			@Override
			public void onFailure(Call call, IOException e) {
				if (listener!=null)
				listener.onFailure(reqCode,"加载失败");
			}
		});
	}
	/**
	 * 网络交互，
	 * @param
	 * @param urlString
	 * @param
	 * @param listener
	 */
	public static void post(Activity activity, String urlString, Map<String,Object> map,
							 final OnResultListener listener) {
		post(activity,urlString,map,0,listener);
	}


	/**
	 * 网络交互，
	 * @param
	 * @param urlString
	 * @param
     * @param listener
     */
	public static void post(Activity activity, String urlString, Map<String,Object> map,final int reqCode,
							final OnResultListener listener) {
		if(activity!=null&&!NetUtils.isConnected(activity)){

			Toasts.show(activity, Contants.getString(activity, R.string.net_error));
			listener.onFailure(reqCode,Contants.getString(context, R.string.net_error));
			return;
		}
		BaseOkHttpClient.Builder builder = BaseOkHttpClient.newBuilder();
		builder.url(urlString);
		String logtstr = urlString+"?";
		map.put("code",HttpUtil.getParam());
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			builder.addParam(entry.getKey(),entry.getValue());
			logtstr+=entry.getKey()+"="+entry.getValue()+"&";
		}
		QTLog.e(logtstr);
		builder.post();
		OkHttpManager.getInstance().tagActivity(activity).request(builder.build(), new BaseCallBack() {
			@Override
			public void onSuccess(Object o) {
				QTLog.e("onSuccess: "+o.toString() );
				Messages messages = JsonUtils.getMessages2(o.toString());

				if (messages.getResult()==1){
					if (listener!=null)
					listener.onSuccess(  reqCode, JsonUtils.JsonParse(messages));
				}else{
					if (listener!=null)
					listener.onFailure(  reqCode,messages.getErr_msg());
				}
			}

			@Override
			public void onError(int code) {
				if (listener!=null)
				listener.onFailure(reqCode,""+code);
			}

			@Override
			public void onFailure(Call call, IOException e) {
				if (listener!=null)
				listener.onFailure(reqCode,"加载失败");
			}
		});
	}

	public static void post2(Activity activity, String urlString, Map<String,Object> map,final int reqCode,
							 final OnResultListener listener) {
		if(activity!=null&&!NetUtils.isConnected(activity)){

			Toasts.show(activity, Contants.getString(context, R.string.net_error));
			listener.onFailure(reqCode,Contants.getString(context, R.string.net_error));
			return;
		}
		BaseOkHttpClient.Builder builder = BaseOkHttpClient.newBuilder();
		builder.url(urlString);
		String logtstr = urlString+"?";
		map.put("code",HttpUtil.getParam());
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			builder.addParam(entry.getKey(),entry.getValue());
			logtstr+=entry.getKey()+"="+entry.getValue()+"&";
		}
		QTLog.e(logtstr);
		builder.post();
		OkHttpManager.getInstance().tagActivity(activity).request(builder.build(), new BaseCallBack() {
			@Override
			public void onSuccess(Object o) {
				QTLog.e("onSuccess: "+o.toString() );
				if (listener!=null)
				listener.onSuccess(  reqCode, o.toString());
			}

			@Override
			public void onError(int code) {
				QTLog.e(""+code);
				if (listener!=null)
				listener.onFailure(reqCode,""+code);
			}

			@Override
			public void onFailure(Call call, IOException e) {
				QTLog.e(""+e.getMessage());
				if (listener!=null)
				listener.onFailure(reqCode,"加载失败");
			}
		});
	}
	public static void cancle(){
		OkHttpManager.getInstance().cancle();
	}
	public static void cancleTag(Activity activity){
		OkHttpManager.getInstance().cancleTag(activity);
	}
	public static void requestBD(Activity activity) {
		HashMap<String,Object> map = new HashMap<>();
		map.put("code", HttpUtil.getParam());
		String mqid = "0";
		String uid = SPUtils.getUserId()+"";
		String regid=uid;
		if (!TextUtils.isEmpty(JPushInterface.getRegistrationID(context))){
			regid = JPushInterface.getRegistrationID(context);
		}

		map.put("users_id", uid+"");
		map.put("register_id", regid+"");

		map.put("service_id",""+ mqid);
		String url = URLAddress.URLBASE + URLAddress.URL_UP_INFO;
		post(activity,url, map, new OnResultListener() {
			@Override
			public void onSuccess(int requestCode, String str) {

			}

			@Override
			public void onFailure(int requestCode, String str) {

			}
		});
	}
}
